package com.pyip.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueueMessage(String message){
        System.out.println("customer get "+message);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue1(String message) throws InterruptedException {
        System.err.println("customer1 get message : [ "+message+" ]");
        Thread.sleep(20);
    }
    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue2(String message) throws InterruptedException {
        System.out.println("customer2 get message : [ "+message+" ]\n");
        Thread.sleep(200);
    }

    // fanout消息接收
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message){
        System.out.println("customer1 get message:["+message+"]");
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message){
        System.out.println("customer2 get message:["+message+"]");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "pyipxt.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}))
    public void listenDirectQueue1(String message){
        System.out.println("customer1 get direct.queue1 message:["+message+"]");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "pyipxt.direct", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}))
    public void listenDirectQueue2(String message){
        System.out.println("customer1 get direct.queue2 message:["+message+"]");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "pyipxt.topic", type = ExchangeTypes.TOPIC),
            key = "#.news"))
    public void listenTopicQueue1(String message){
        System.out.println("customer1 get topic.queue1 message:["+message+"]");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "pyipxt.topic", type = ExchangeTypes.TOPIC),
            key = "china.#"))
    public void listenTopicQueue2(String message){
        System.out.println("customer2 get topic.queue2 message:["+message+"]");
    }
}
